/*
 * BMA180Controller class definition
 *
 */
#ifndef BMA180CONTROLLER_H_
#define BMA180CONTROLLER_H_

#include <wiring.h> // for boolean and byte

class BMA180Controller
{
  public:
    enum ResultType { SUCCESS, FAILURE } ;
    struct AccelDataSet {
      float accel_x; // acceleration in the x axis measured in "g"s 
      float accel_y; // acceleration in the y axis measured in "g"s 
      float accel_z; // acceleration in the z axis measured in "g"s 
      unsigned long time; // milliseconds since reboot
    };
     

    /* 
     *  Constructor.  This only initialized the class.  it
     *  does not actually initialize the interface.
     */
    BMA180Controller();
  
    /*  This routine intitializes the interface with the 
     *  BMA180 triple axis accelerometer via the I2C interface.
     *
     *  return: ResultType
     */
    ResultType init();
    
    /*  This routine intitializes the interface with the 
     *  BMA180 triple axis accelerometer via the I2C interface.
     *
     *  return: ResultType
     */
    ResultType readData();
    
    /*  This routine returns the last measured data.
     *
     *  return: last retrieved data
     */
    AccelDataSet getData();
    
    /*
     *  This routine returns the current initialization
     *  status.
     *
     */
    boolean isInitialized()
    {
      return this->initialized;      
    }
    
  private:
  
    /*
     *  Helper routine to get a byte of data from the device
     *
     *  parameters:
     *    reg_addr, in, address of the register to get the byte from
     *    data, out, data retrieved from the device
     *
     *  return: results from endTransmission
     *
     */
    byte getAByte(byte reg_addr, byte & data);
    
    
    /*
     *  Helper routine to get acceleration registers
     *
     *  parameters:
     *    reg_lsb_addr, in, LSB address of the register to get the byte from
     *    reg_msb_addr, in, MSB address of the register to get the byte from
     *    new_data, out, indicates if the new_data bit was set in the LSB
     *    acceleration, out, the reformatted acceleration data
     *
     *  return: results from endTransmission
     *
     */
    byte getAcceleration(
            byte reg_lsb_addr, 
            byte reg_msb_addr, 
            boolean & new_data, 
            float & acceleration);
  
  
    // indicates the I2C address of our device
    byte address;
    
    // indicates that the interface has been initialized
    boolean  initialized; 
    
    // maximum range of values (in G's) that was configured
    // into the sensor
    float range_lsb;                   
  
    // The previous time that data was read from the sensor
    unsigned long last_read_time;

    // The last data that was read
    AccelDataSet data_set;

};

#endif // BMA180CONTROLLER_H_
